package com.xgpro.mapper.archives;

import com.xgpro.domain.archives.dto.*;
import com.xgpro.domain.archives.entity.JobTitle;
import com.xgpro.domain.archives.entity.PerForMance;
import com.xgpro.domain.archives.entity.Rank;
import com.xgpro.domain.archives.vo.Archives;
import com.xgpro.domain.talentintroduction.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


/**
 * @program: xgpro__dev
 * @Description:
 * @Author: wangyang
 * @Date: 2019-09-20 23:39
 **/
public interface ArchivesMapper {


    @Select("<script>" +
            "select id as deptId, name as deptName , introduce "+
            "From t_deptment"+
            "<where>" +
            "<if test=\"deptId != null\" >"+
            "id=#{deptId}"+
            "</if>"+
            "</where>" +
            "</script>")
    Deptment getDeptment(@Param("deptId") Integer deptId);



    @Select("<script>" +
            "SELECT id  , name   ,uptime,sort,queue FROM `t_rank`"+
            "<where>" +
            "<if test=\"rankId != null\" >"+
            "id=#{rankId}"+
            "</if>"+
            "</where>" +
            "</script>")
    Rank getRank(@Param("rankId") Integer rankId);


    @Select("SELECT id as rankId , name as rankName , uptime ,sort, queue FROM `t_rank` WHERE queue = #{queue} ORDER BY sort DESC")
    List<Ranks> getRanks(@Param("queue") String queue);



    @Select("<script>" +
            "SELECT  id as eduId,school,"+
            "gra_time as graTime,major as eduMajor,edu_kind as eduKind FROM `t_edu`"+
            "<where>" +
            "<if test=\"eduId != null\" >"+
            "id=#{eduId}"+
            "</if>"+
            "</where>" +
            "</script>")
    Eduu getEdu(@Param("eduId") Integer eduId);


    @Select("<script>" +
            "select t_job_title.id as id,t_job_title.major_type as majorType,t_job_title.certificate_time as certificateTime,t_job_title.valid_time as validTime,"+
            "t_job_title.license as license,t_job_title.user_id as userId" +
            " from t_user left join t_job_title on t_user.id = t_job_title.user_id " +
            "where t_user.id=#{id}" +
            " ORDER BY " +
            " certificate_time" +
            " DESC LIMIT 1" +
            "</script>")
    JobTitle getTitleInfo(@Param("id") Integer id);



    @Select("select id,name,license_time as licenseTime ,patent_number as patentNumber,patent_post AS patentPost,patent_type AS patentType ,user_id as userId ," +
            " patent_person AS patentPerson,patent_internal_number AS patentInternalNumber" +
            " from `t_patent`"+
            "where user_id=#{id} and patent_type=#{type}")
    List<Patentt> getPanentInfo(@Param("id") Integer id, @Param("type") Integer type);


    @Select("SELECT id,name,license_time AS licenseTime ,user_id as userId ,patent_number as patentNumber ,patent_post AS patentPost, patent_type AS patentType ," +
            " patent_person AS patentPerson,patent_internal_number AS patentInternalNumber" +
            " FROM t_patent where user_id=#{id} ")
    List<Patentt> getPanents(@Param("id") Integer id);


    @Select("select id,name,license_time as licenseTime ,patent_type AS patentType ,patent_post AS patentPost,patent_number as patentNumber,user_id as userId," +
            " patent_person AS patentPerson,patent_internal_number AS patentInternalNumber" +
            " from `t_patent`"+
            "where id=#{id}")
    Patentt getPanent(@Param("id") Integer id);


    @Select("select id,project_name as projectName,project_level as projectLevel ,project_post as projectPost , "+
            "project_introduce as projectIntroduce ,project_achievement as projectAchievement , project_number as projectNumber," +
            "project_starttime as projectStarttime ,project_endtime as projectEndtime ,user_id as userId " +
            "from t_project " +
            "where user_id=#{id} and project_level=#{projectlevel}")
    List<Projectt> getProjects(@Param("id") Integer id, @Param("projectlevel") Integer projectlevel);





    @Select("select id,project_name as projectName,project_level as projectLevel ,project_post as projectPost , "+
            "project_introduce as projectIntroduce ,project_achievement as projectAchievement ,project_number as projectNumber," +
            "project_starttime as projectStarttime ,project_endtime as projectEndtime,user_id as userId " +
            "from t_project " +
            "where id=#{id}")
    Projectt getProject(@Param("id") Integer id);


    @Select("select  id,per_time as perTime,perdept,introduce " +
            "from t_performance " +
            "where "+
            "user_id=#{id}" +
            " ORDER BY" +
            " create_time" +
            " DESC LIMIT 3")
    List<PerForMance> getPer(@Param("id") Integer id);

    @Select("select b.course_time " +
            "from t_user_course a left join t_course b on a.course_id = b.id " +
            "where a.status = 1 and a.student_id=#{id}")
    List<Integer> getClassTime(@Param("id") Integer id);




    @Select("select course_time " +
            "from  t_course " +
            "where status = 1 and tea_id=#{id}")
    List<Integer> getAttendanceTime(@Param("id") Integer id);


    @Select("<script>" +
            "SELECT  name FROM `t_rank`"+
            "</script>")
    List<String> getRankName();

    @Select("<script>" +
            "select  name From t_deptment"+
            "</script>")
    List<String> getDeptmentName();




    @Select("<script>" +
            " select t_user.id as userId,t_user.username,t_rank.name AS rankName ,t_deptment.name as deptName ,t_user.job AS job ,t_edu.edu_kind AS eduKind " +
            " from t_user left join  t_edu on t_user.edu_id = t_edu.id left join t_deptment on t_user.dept_id=t_deptment.id left join t_rank on t_user.rank_id=t_rank.id" +
            "<where>" +
            " <if test=\"name != null and name!='' \"> " +
            "  and t_user.username like CONCAT('%',#{name},'%')" +
            " </if>" +
            " <if test=\"deptName != null and deptName!='' \"> " +
            "     t_deptment.name like CONCAT('%',#{deptName},'%')" +
            " </if>" +
            " <if test=\"eduKind != null and eduKind!='' or eduKind==0 \"> " +
            "  and t_edu.edu_kind=#{eduKind}  " +
            " </if>" +
            "</where>" +
            "</script>")
    List<Archives> getArchives(@Param("name")String name,@Param("deptName")String deptName,@Param("eduKind")Integer eduKind);


    @Select(" select id,username,user_id as userId,dept_name as deptName,upper_rank_name as upperRankName,next_rank_name as nextRankName,hired_date as hiredDate," +
            " status,msg,creation_time as creationTime,update_time as updateTime" +
            " from t_rank_approval" +
            " where "+
            " user_id=#{id}" +
            " ORDER BY" +
            " creation_time" +
            " DESC")
    List<ApprovalRank> select(@Param("id")Integer id);







   @Update("<script>" +
           " update t_user " +
           " <trim prefix='set' suffixOverrides=','> " +
           " <trim prefix='rank_id =case' suffix='end,'>" +
           " <foreach collection='list' item='user' index='index'   >" +
           " <if test=\"user.rankId != null and user.rankId!='' \">"+
           " when id=#{user.id} then #{user.rankId}" +
           " </if>"+
           " </foreach>" +
           " </trim>" +
           "<trim prefix='rank_update =case' suffix='end,'> " +
           "<foreach collection='list' item='user' index='index' >" +
           " when id=#{user.id} then #{user.rankUpdate} " +
           "</foreach>\n" +
           "</trim>"+
           " </trim>" +
           "  where id in "+
           " <foreach collection='list' item='user' index='index' separator=',' open='(' close=')'>" +
           "      #{user.id} " +
           "</foreach>"+
           " </script>"
            )
    int update(@Param("list")List<User> list);
}